package singleton

import (
	"fmt"
	"math/rand"
	"time"
)

func TimeBigSmall(big, small time.Time, info string) (bool, string) {
	d := big.Sub(small)
	if d.Seconds() < -1 {
		WriteToValidateErrorFile(info)
		return false, generateNewDay(small)
	}
	return true, ""
}

func TimeBigSmalls(big time.Time, info string, smalls ...time.Time) (bool, string) {
	maxTime := smalls[0]
	for i, v := range smalls {
		if i == 0 {
			continue
		}
		if v.After(maxTime) {
			maxTime = v
		}
	}

	d := big.Sub(maxTime)
	if d.Seconds() < -1 {
		WriteToValidateErrorFile(info)
		return false, generateNewDay(maxTime)
	}
	return true, ""
}

func generateNewDay(d time.Time) string {
	h := d.Hour()
	var delayHour int = 24
	if h < 3 {
	} else if h < 6 {
		delayHour = 21
	} else if h < 9 {
		delayHour = 18
	} else if h < 12 {
		delayHour = 15
	} else if h < 15 {
		delayHour = 12
	} else if h < 18 {
		delayHour = 9
	} else {
		delayHour = 6
	}

	// 3小时
	d = d.Add(time.Duration(time.Hour * time.Duration(delayHour)))
	d = d.Add(time.Duration(time.Second * time.Duration(rand.Intn(3*3600))))
	return d.Format("2006-01-02 15:04:05")
}

func TimeGlobalCheck2(info string, as time.Time) (bool, string) {

	h := as.Hour()
	if h < 8 {
		WriteToValidateErrorFile(fmt.Sprintf("TimeGlobalCheck: %s, %v,h:%d", info, as, h))
		as = as.Add(time.Hour * 8).Add(time.Second * time.Duration(rand.Intn(3500)))
		return false, as.Format("2006-01-02 15:04:05+00")
	}

	return true, ""
}
